<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css";>
        #box1 {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }
    </style>
    <script type="text/javascript">

        window.onload = function () {
    
            var box1 = document.getElementById("box1");
            box1.onmousemove = function (event) {
                event = event || window.event;
                /* 
                    clientX,clientY
                    用于获取鼠标当前的可见窗口的目标
                */

                /* 
                    pageX，pageY可以获取鼠标相对于当前页面的坐标
                */
                // var left = event.clientX;
                // var top = event.clientY;
                var left = event.pageX;
                var top = event.pageY;
    
                box1.style.left = left+"px";
    
                box1.style.top = top+"px";
            };
        }
    </script>
    
</head>

<body>
    <div id="box1"></div>

</body>

</html>